package utils
{
	import manager.ObjectPool;
	
	public class LoaderManage
	{	
		private static var instance:LoaderManage;
		
		public function LoaderManage()
		{
			if(instance)throw new Error("Singerleton!");
		}
		
		public static function getInstance():LoaderManage{
			return instance ||=new LoaderManage();
		}
		
		public function loaderContent(url:String,complete:Function,isBackURL:Boolean = false):void{
			var gl:GameLoader = new GameLoader();
			gl.loading(url,complete,isBackURL);
		}
		
	}
}
import data.Globals;

import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;

import manager.ObjectPool;

import utils.GameUtils;

class GameLoader extends Loader{
	private var callback:Function;
	
	private var loader:Loader;
	
	private var queue:Array;
	
	private var urlReq:URLRequest;
	
	private var _isBackURL:Boolean;
	
	
	public function GameLoader():void{
		
	}
	
	public function loading(url:String,complete:Function,isBackURL:Boolean = false):void{
		callback = complete;
		_isBackURL = isBackURL;
		loader = new Loader();
		urlReq = new URLRequest(url+"?="+Globals.version);
		var loaderContect:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
		loader.load(urlReq,loaderContect);
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete);
		loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,io_error);
	}
	
	private function io_error(event:IOErrorEvent):void
	{
		GameUtils.destoyEvent(event);
		loader =null;
		urlReq = null;
	}
	
	private function loaderComplete(event:Event):void
	{
		if(_isBackURL){
			callback(event,urlReq.url);
		}else{
			callback(event);
		}
		
		GameUtils.destoyEvent(event);
		loader =null;
		urlReq = null;
	}
	
}